const DATA = require("../test/210305-gaoshoudata+tsne.json");
const mongoConnected = require("../lib/mongoose_connect");
const mongoModels = require("../lib/mongoose_schemas");
const DPlib = require("../lib/Douglas-Peucker");
mongoConnected.then(async $ => {
  // debugger
  for (let ddd of DATA) {
    let account_id = ddd.account_id;
    let match_id = ddd.match_id;
    let doc = await mongoModels.TB21_gaoshouguiji.findOne({ account_id, match_id });
    let raw_trajectory = doc.raw_points;
    let simplified = DPlib.simplifyDouglasPeucker(raw_trajectory, (100 * 100) ** 2);
    doc.simplified_points = simplified;
    doc.map_name = "Savage_Main";
    await doc.save();
    // debugger
  }
  debugger
});
_ = async function () {

  return
  for (let ddd of DATA) {
    let account_id = ddd.account_id;
    let match_id = ddd.match_id;
    let player_name = ddd.player_name;
    let team_size = ddd.team_size, ranking = ddd.ranking;
    let evParachutelanding = await mongoModels.Event.findOne({
      match_id,
      _T: "LogParachuteLanding",
      "character.accountId": account_id
    });
    let landingTime = evParachutelanding._D;
    let evs = await mongoModels.Event.find({
      match_id: match_id,
      _D: {
        $gte: landingTime
      },
      $or: [
        { "killer.accountId": account_id },
        { "attacker.accountId": account_id },
        { "victim.accountId": account_id },
        { "assistant.accountId": account_id },
        { "character.accountId": account_id },
      ]
    }).sort({
      _D: 1
    });
    let locations = evs.map(ev => {
      if (ev._doc.character) {
        return ev._doc.character.location
      }
      if (ev._doc.killer && ev._doc.killer.accountId == account_id) {
        return ev._doc.killer.location
      }
      if (ev._doc.attacker && ev._doc.attacker.accountId == account_id) {
        return ev._doc.attacker.location
      }
      if (ev._doc.victim && ev._doc.victim.accountId == account_id) {
        return ev._doc.victim.location
      }
      if (ev._doc.assistant && ev._doc.assistant.accountId == account_id) {
        return ev._doc.assistant.location
      }
    });
    // debugger
    for (let i = locations.length - 1; i >= 1; i--) {
      if (JSON.stringify(locations[i]) == JSON.stringify(locations[i - 1])) {
        locations[i].x = 0;
        locations[i].y = 0;
        locations[i].z = 0;
        // console.log("重复")
      }
    }
    // debugger
    locations = locations.filter(e => !(e.x == 0 && e.y == 0 && e.z == 0));
    let record = new mongoModels.TB21_gaoshouguiji({
      raw_points: locations,
      account_id: account_id,
      match_id: match_id,
      player_name: player_name,
      ranking: ranking,
      team_size: team_size
    });
    await record.save();
    // debugger
  }
  debugger
  process.exit();
};